package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetFavorListResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import glideHelp.GlideHelper;
import ui.NavigateBar;

/**
 * Created by ASUS on 2017/11/6.
 */

public class FavorAdapter extends BaseAdapter {
    private ArrayList<GetFavorListResponse> getFavorListResponses;
    private Context mContext;
    private NavigateBar navigateBar;
    public FavorAdapter(ArrayList<GetFavorListResponse> getFavorListResponses, Context context, NavigateBar navigateBar){
        this.getFavorListResponses = getFavorListResponses;
        mContext = context;
        this.navigateBar = navigateBar;
    }

    @Override
    public int getCount() {
        return getFavorListResponses.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    public boolean isAllSelect(){
        for(GetFavorListResponse favorResponse: getFavorListResponses){
            if(!favorResponse.isChoose()){
                return false;
            }
        }
        return true;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.favor_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        GlideHelper.loadImage(mContext, getFavorListResponses.get(i).getImage_url(), holder.logo);
        holder.name.setText(getFavorListResponses.get(i).getName());
        if(getFavorListResponses.get(i).isChoose()){
            holder.choose.setImageResource(R.mipmap.check_ok);
        } else {
            holder.choose.setImageResource(R.mipmap.check_none);
        }
        if(navigateBar.getOperateTitle().equals("取消")){
            holder.choose.setVisibility(View.VISIBLE);
        } else {
            holder.choose.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.choose)
        ImageView choose;
        @BindView(R.id.logo)
        ImageView logo;
        @BindView(R.id.name)
        TextView name;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
